package com.miyu.common.service.book.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;
import com.miyu.common.pojo.book.BookOperate;
import com.miyu.common.pojo.book.BookPage;
import com.miyu.common.service.book.BookService;
import com.miyu.common.utils.commonUtils.Comutils;
import com.miyu.common.utils.page.BookPageJx;
import com.miyu.common.utils.redis.redisUtils.RedisCache;
import com.miyu.common.utils.redis.redisUtils.util.RedisListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * 书籍服务
 * @author longm
 * @date 2022/11/19
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private  RedisCache  redisCache;

    /**
     * 书页jx
     * @param bookPage 本页面
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject BookPageJX(BookPage bookPage) {

        String key="";
        if(StringUtils.isBlank(bookPage.getKey())){
            //随机key值
            key = Comutils.getUUID();
            bookPage.setKey(key);
        }
        if(bookPage.getOperate().isEmpty()){
            return null;
        }
        if(bookPage.getUrl().isEmpty()){
            return null;
        }
        BookPageJx.bookPageJX(bookPage);
        return null;
    }

    /**
     * 查询搜索书
     * @param bookPage 本页面
     * @return {@link JSONArray}
     */
    @Override
    public JSONArray querySearchBook(BookPage bookPage) {
        //随机key值
        String key="";
        key = Comutils.getUUID();
        bookPage.setKey("book:search:"+key);
        bookPage.setOperate(BookOperate.SEARCH.getCode());
        BookPageJX(bookPage);
        String ss = redisCache.get(bookPage.getKey());
        if(bookPage.isRemove()){
            redisCache.remove(bookPage.getKey());
        }
        JSONArray rs = JSONArray.parseArray(ss, JSONReader.Feature.IgnoreSetNullValue);
        return rs;
    }
    /**
     * 查询书细节
     * @param bookPage 本页面
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject queryBookDetail(BookPage bookPage) {
        if(StringUtils.isBlank(bookPage.getKey())){
            bookPage.setKey("book:detail:"+Comutils.getUUID());
        }
        bookPage.setOperate(BookOperate.DETAIL.getCode());
        BookPageJX(bookPage);
        String ss = redisCache.get(bookPage.getKey());
        if(bookPage.isRemove()){
            redisCache.remove(bookPage.getKey());
        }
        JSONObject rs = JSONObject.parseObject(ss);
        return rs;
    }
    /**
     * 查询书章
     * @param bookPage 本页面
     * @return {@link JSONObject}
     */
    @Override
    public JSONArray queryBookChapter(BookPage bookPage) {
        String key="";
        key = Comutils.getUUID();
        bookPage.setKey("book:chapter"+":"+key);
        bookPage.setOperate(BookOperate.CHAPTER.getCode());
        BookPageJX(bookPage);
        String data = redisCache.get(bookPage.getKey());
        if(bookPage.isRemove()){
            redisCache.remove(bookPage.getKey());
        }
        JSONArray rs = JSONArray.parseArray(data, JSONReader.Feature.IgnoreSetNullValue);
        return rs;
    }

    /**
     * 查询书读者
     * @param bookPage 本页面
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject queryBookReader(BookPage bookPage) {
        String key="";
        key = Comutils.getUUID();
        bookPage.setKey("book:reader"+":"+key);
        bookPage.setOperate(BookOperate.READER.getCode());
        BookPageJX(bookPage);
        String data = redisCache.get(bookPage.getKey());
        if(bookPage.isRemove()){
            redisCache.remove(bookPage.getKey());
        }
        return JSONObject.parseObject(data);
    }

    /**
     * 收集书
     * @param bookPage 本页面
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject collectBook(BookPage bookPage) {
        bookPage.setOperate(BookOperate.COLLECT.getCode());
        return BookPageJX(bookPage);
    }

    /**
     * 规则校验取值
     * 单独取值 不参与规则
     * @param bookPage 本页面
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject checkBookRule(BookPage bookPage) {
        //随机key值
        String key="";
        key = Comutils.getUUID();
        bookPage.setKey(bookPage.getKey()+":"+key);
        bookPage.setOperate(BookOperate.CHECKRULE.getCode());
        BookPageJX(bookPage);
        String ss = redisCache.get(bookPage.getKey());
        if(bookPage.isRemove()){
            redisCache.remove(bookPage.getKey());
        }
        JSONObject rs = JSONObject.parseObject(ss.toString());

        return rs;
    }
}
